<template>
    <el-menu :unique-opened='true'>
        <template v-for="(item,index) in NacList" :key="index">
            <el-sub-menu :index="index.toString()" v-if="item.childList&&item.isLeft==1" default-openeds="1">
                <template #title>
                    <el-icon>
                        <component :is="item.icon"></component>
                    </el-icon>
                    {{ item.title }}
                </template>
                <el-menu-item v-for="(cItem,cIndex) in item.childList" :key="cIndex" :index="index+'-'+cIndex">
                    <router-link :to="cItem.path">
                        <el-icon>
                            <component :is="cItem.icon"></component>
                        </el-icon>
                        {{ cItem.title }}
                    </router-link>
                </el-menu-item>
            </el-sub-menu>
            <el-menu-item v-else-if="item.isLeft==1" class="aaa" :index="index.toString()">
                <router-link :to="item.path">
                    <el-icon>
                        <component :is="item.icon"></component>
                    </el-icon>
                    {{ item.title }}
                </router-link>
            </el-menu-item>
        </template>
    </el-menu>
</template>

<script setup>
import { onMounted, ref } from 'vue';
import { RouterLink } from 'vue-router';
import { getMenuList } from './Nav/Nav';
let NacList = ref([])
let token = sessionStorage.getItem("token")
onMounted(() => {
    getMenuListFun()
})
let getMenuListFun = () => {
    getMenuList({token:token})
    .then(res => {
        if(res.code == 200){
            // console.log(res.dataset);  
            NacList.value = bubbleSort(res.dataset) 
        }
    })
}
function bubbleSort(arr) {
  const newArr = [...arr];
  for (let i = 0; i < newArr.length - 1; i++) {
    if(newArr[i].childList.length>0){
      bubbleSort(newArr[i].childList);
    }
    for (let j = 0; j < newArr.length - 1 - i; j++) {
      if (newArr[j].orders < newArr[j + 1].orders) {
        [newArr[j], newArr[j + 1]] = [newArr[j + 1], newArr[j]];
      }
    }
  }
  return newArr;
}


</script>

<style lang="less" scoped>
.el-menu{
    width:100%;
    height:100%;
    background-color: #314155;
    :deep(.el-sub-menu__title){
        color:#fff;
        font-size: 12px;
    }
}
</style>